﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

   <ControlTemplate TargetType="ToggleButton"
                    x:Key="ComboBoxToggleButtonTemplate">
      <Grid>
         <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="30" />
         </Grid.ColumnDefinitions>
         <Border Grid.ColumnSpan="2"
                 Name="PART_Background"
                 BorderThickness="1"
                 BorderBrush="{DynamicResource MainBorderBrush}"
                 Background="{DynamicResource BackgroundBrush}" />

         <Border Grid.Column="1"
                 Margin="1"
                 Name="PART_ButtonBorder"
                 BorderThickness="0"
                 BorderBrush="{DynamicResource MainBorderBrush}"
                 Background="{DynamicResource BackgroundBrush}" />

         <Path Name="PART_Arrow"
               Grid.Column="1"
               StrokeThickness="1"
               Data="M0,0 L4,6 L8,0 Z"
               VerticalAlignment="Center"
               HorizontalAlignment="Center"
               Fill="{DynamicResource AlternateBrush}"
               Stroke="{DynamicResource AlternateBrush}" />

         <Border Grid.Column="0"
                 Grid.ColumnSpan="2"
                 Name="PART_Border"
                 BorderThickness="1"
                 BorderBrush="{DynamicResource MainBorderBrush}" />
      </Grid>
      <ControlTemplate.Triggers>
         <Trigger Property="UIElement.IsMouseOver"
                  Value="True">
            <Setter TargetName="PART_ButtonBorder"
                    Property="Background"
                    Value="{DynamicResource SecondaryBrush}" />
            <Setter TargetName="PART_ButtonBorder"
                    Property="BorderThickness"
                    Value="1,0,0,0" />
         </Trigger>

         <Trigger Property="ToggleButton.IsChecked"
                  Value="True">
            <Setter TargetName="PART_ButtonBorder"
                    Property="Background"
                    Value="{DynamicResource SecondaryClickBrush}" />
            <Setter TargetName="PART_Arrow"
                    Property="Fill"
                    Value="{DynamicResource AlternateClickBrush}" />
            <Setter TargetName="PART_Arrow"
                    Property="Stroke"
                    Value="{DynamicResource AlternateClickBrush}" />
         </Trigger>
         <Trigger Property="UIElement.IsEnabled"
                  Value="False">
            <Setter TargetName="PART_Background"
                    Property="Background"
                    Value="{DynamicResource DisabledBackgroundBrush}" />
            <Setter TargetName="PART_ButtonBorder"
                    Property="Background"
                    Value="{DynamicResource DisabledBackgroundBrush}" />
            <Setter Property="Foreground"
                    Value="{DynamicResource DisabledForegroundBrush}" />
            <Setter TargetName="PART_Arrow"
                    Property="Fill"
                    Value="{DynamicResource DisabledForegroundBrush}" />
            <Setter TargetName="PART_Arrow"
                    Property="Stroke"
                    Value="{DynamicResource DisabledForegroundBrush}" />
         </Trigger>
      </ControlTemplate.Triggers>
   </ControlTemplate>

   <Style TargetType="{x:Type ComboBox}">
      <Setter Property="SnapsToDevicePixels"
              Value="True" />
      <Setter Property="OverridesDefaultStyle"
              Value="True" />
      <Setter Property="ScrollViewer.HorizontalScrollBarVisibility"
              Value="Auto" />
      <Setter Property="ScrollViewer.VerticalScrollBarVisibility"
              Value="Auto" />
      <Setter Property="ScrollViewer.CanContentScroll"
              Value="True" />
      <Setter Property="TextElement.Foreground"
              Value="Black" />
      <Setter Property="FocusVisualStyle"
              Value="{x:Null}" />
      <Setter Property="Template">
         <Setter.Value>
            <ControlTemplate TargetType="ComboBox">
               <Grid>
                  <ToggleButton Name="ToggleButton"
                                ClickMode="Press"
                                Focusable="False"
                                IsChecked="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                                Template="{StaticResource ComboBoxToggleButtonTemplate}" />

                  <ContentPresenter Name="ContentSite"
                                    Margin="5, 3, 33, 3"
                                    IsHitTestVisible="False"
                                    HorizontalAlignment="Left"
                                    VerticalAlignment="Center"
                                    Content="{TemplateBinding SelectionBoxItem}"
                                    ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                    ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" />

                  <TextBox Name="PART_EditableTextBox"
                           Focusable="True"
                           Visibility="Hidden"
                           Margin="3, 3, 33, 3"
                           Background="Transparent"
                           HorizontalAlignment="Left"
                           VerticalAlignment="Center"
                           IsReadOnly="{TemplateBinding IsReadOnly}">
                     <TextBox.Template>
                        <ControlTemplate TargetType="TextBox">
                           <Border Name="PART_ContentHost"
                                   Focusable="False" />
                        </ControlTemplate>
                     </TextBox.Template>
                  </TextBox>

                  <!-- Popup showing items -->
                  <Popup Name="Popup"
                         Placement="Bottom"
                         Focusable="False"
                         AllowsTransparency="True"
                         IsOpen="{TemplateBinding ComboBox.IsDropDownOpen}"
                         PopupAnimation="Slide">
                     <Grid Name="DropDown"
                           SnapsToDevicePixels="True"
                           MinWidth="{TemplateBinding FrameworkElement.ActualWidth}"
                           MaxHeight="{TemplateBinding ComboBox.MaxDropDownHeight}">
                        <Border Name="DropDownBorder"
                                Background="White"
                                BorderThickness="1,1,1,1"
                                BorderBrush="{DynamicResource MainBorderBrush}" />
                        <ScrollViewer SnapsToDevicePixels="True">
                           <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained" />
                        </ScrollViewer>
                     </Grid>
                  </Popup>
               </Grid>
               <ControlTemplate.Triggers>
                  <Trigger Property="ItemsControl.HasItems"
                           Value="False">
                     <Setter Property="FrameworkElement.MinHeight"
                             TargetName="DropDownBorder"
                             Value="95" />
                  </Trigger>
                  <Trigger Property="UIElement.IsEnabled"
                           Value="False">
                     <Setter Property="TextElement.Foreground"
                             Value="{DynamicResource DisabledForegroundBrush}" />
                  </Trigger>
                  <Trigger Property="ItemsControl.IsGrouping"
                           Value="True">
                     <Setter Property="ScrollViewer.CanContentScroll"
                             Value="False" />
                  </Trigger>
                  <Trigger Property="ComboBox.IsEditable"
                           Value="True">
                     <Setter Property="KeyboardNavigation.IsTabStop"
                             Value="False" />
                     <Setter Property="UIElement.Visibility"
                             TargetName="PART_EditableTextBox"
                             Value="Visible" />
                     <Setter Property="UIElement.Visibility"
                             TargetName="ContentSite"
                             Value="Hidden" />
                  </Trigger>
               </ControlTemplate.Triggers>
            </ControlTemplate>
         </Setter.Value>
      </Setter>
   </Style>
   
</ResourceDictionary>